package felix.rule.js.script;

public class NodeScriptFactory {

    public static final String ID = "id";
    public static final String MSG = "msg";
    public static final String METADATA = "metadata";
    public static final String MSG_TYPE = "msgType";
    public static final String RULE_NODE_FUNCTION_NAME = "ruleNodeFunc";

    private static final String JS_WRAPPER_PREFIX_TEMPLATE = "function %s(id, msgStr, metadataStr, msgType) { " +
            "    var msg = JSON.parse(msgStr); " +
            "    var metadata = JSON.parse(metadataStr); " +
            "    return JSON.stringify(%s(id, msg, metadata, msgType));" +
            "    function %s(%s, %s, %s, %s) {";
    private static final String JS_WRAPPER_SUFFIX = "\n}" +
            "\n}";


    public static String generateRuleNodeScript(String functionName, String scriptBody) {
        String jsWrapperPrefix = String.format(JS_WRAPPER_PREFIX_TEMPLATE,
                functionName,
                RULE_NODE_FUNCTION_NAME, RULE_NODE_FUNCTION_NAME, ID, MSG, METADATA, MSG_TYPE);
        return jsWrapperPrefix + scriptBody + JS_WRAPPER_SUFFIX;
    }

}
